<?php

namespace App\Backend\Controllers;

use App\Backend\Service\ContentModerationService;
use App\Backend\Controllers\BaseController;
use Illuminate\Http\Request;

class ImageCheckController extends BaseController
{
    protected $moderationService;

    public function __construct(ContentModerationService $moderationService)
    {
        $this->moderationService = $moderationService;
    }

    public function checkImage(Request $request)
    {
        
   /*     [
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,webp|max:10240'
        ];*/
        $image = $request->file('image');
        $tempPath = $image->getRealPath();



        $result =$this->moderationService->checkImageAdvanced($tempPath);

        if (!$result['success']) {
            return response()->json([
                'status' => 'error',
                'message' => $result['error']
            ], 500);
        }

        return response()->json([
            'status' => 'success',
            'image_url' => $result['image_url'],
            'results' => $result['results']
        ]);
    }

    /**
     * 处理图片检测请求
     */
    public function checkImage11(Request $request)
    {



        $request->validate([
            'image_path' => 'required|string'
        ]);

        $imagePath = $request->json('image_path');
        $imageUrl = $this->moderationService->getOssImageUrl($imagePath);

        // 执行图片安全检测
        $result = $this->moderationService->checkImageAdvanced($imageUrl);
        $parsedResult = $this->moderationService->parseResult($result);

        $msg=[
            'image_url' => $imageUrl,
            'result' => $parsedResult
        ];
        $this->success($msg);
    }
}
